let methodList = [];
let stringList = [];

function exportCode(customCache) {
    methodList.length = 0;
    stringList.length = 0;
    let rootList = customCache.filter((item) => {
        return item.data.parent == null
    });

    rootList.forEach((item) => {
        let code = doExport(item);
        console.log(code);
    });
}

function resetAndExport(cache) {
    methodList.length = 0;
    stringList.length = 0;
    // let code = doExport(cache);
    let code;
    if (isAndroid) {
        code = exportXmlByType(cache);
    } else {
        code = doExport(cache);
    }

    if (methodList.length > 1 || (methodList.length == 1 && cache.name == null)) {
        code = `
            Widget body() {
                return ${code};
            }
        `
        
        methodList.forEach((item) => {
            code += item;
        });
    } else if (methodList.length == 1) {
        code = methodList[0];
    }

    return code;
}

function doExport(cache) {
    cache.type = cache.type || figure(cache.data);
    let code = exportByType(cache);
    if (cache.name) {
        let method = `
        Widget ${cache.name}() {
            return ${code};
        }
        `;

        methodList.push(method);
        code = `${cache.name}()`;
    }

    return code;
}

function exportByType(cache) {
    let code = "";

    switch (cache.type.toLowerCase()) {
        case "column":
            code = columnCode(cache);
            break;
        case "row":
            code = rowCode(cache);
            break;
        case "stack":
            code = stackCode(cache);
            break;
        case "container":
            code = containerCode(cache);
            break;
        case "text":
            code = genTextCode(cache);
            break;
        case "image":
            code = imageCode(cache);
            break;
        case "sizedbox":
            code = sizedBoxCode(cache);
            break;
        default:
            break;
    }
    return code;
}